package avicit.bdp.dds.bundle;

import avicit.bdp.core.util.json.JSONUtils;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class GenerateSqlFromJson {

	private static final String SQL_TEMP =
			"INSERT ignore INTO `bdp_dds_task_nodes`" +
					"(`id`, `name`, `code`, `type`, `icon`, `type_id`, `class_name`, `param_class_name`, " +
					"`color`, `remark`, `created_by`, `creation_date`, `last_updated_by`, `last_update_date`, " +
					"`last_update_ip`, `version`, `org_identity`, `secret_level`, `order_by`, `exec_class_name`, " +
					"`program_type`, `params`, `node_file_path`, `showed`) " +
					"VALUES " +
					"(" +
					"'%s'," + "'%s'," + "'%s'," + "'%s'," + "'%s'," + "'%s'," + "'%s'," +
					"'%s'," + "'%s'," + "'%s'," + "'%s'," + "'%s'," + "'%s'," + "'%s'," +
					"'%s'," + "'%s'," + "'%s'," + "'%s'," + "%d," + "'%s'," + "'%s'," +
					"'%s'," + "'%s'," + "%d" +
					");";


	public static void main(String[] args) throws Exception {
		File file = new File("D:\\IdeaProjects\\avicit-platform6-bdp2\\bdp-sparkflow\\src\\main\\scala\\avicit\\bdp\\bundle");
		showAllFile(file);
	}


	public static void showAllFile(File dir) throws Exception {
		File[] fs = dir.listFiles();
        for (File tmpFile : fs) {
            if (tmpFile.isDirectory()) {
                try {
                    showAllFile(tmpFile);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    if (!tmpFile.getName().endsWith("json")) {
                        continue;
                    }

                    char[] buffer = new char[10000];
                    // 一次读一个字节
                    InputStream in = new FileInputStream(tmpFile);
                    InputStreamReader input = new InputStreamReader(in);
                    int len = input.read(buffer);
                    String text = new String(buffer, 0, len);
                    JSONObject jsonNodes = JSONUtils.parseObject(text);

                    String typeId = jsonNodes.getString("typeId");
                    if (StringUtils.isEmpty(typeId)) {
                        continue;
                    }

                    String format = String.format(SQL_TEMP,
                            jsonNodes.getString("code"),
                            jsonNodes.getString("name"),
                            "SPARKFLOW",
                            "sparkFlow",
                            "",
                            typeId,
                            jsonNodes.getString("bundle"),
                            "NULL",
                            "#e53939",
                            "NULL",
                            "1",
                            "2021-07-15 09:31:22",
                            "1",
                            "2021-07-15 09:31:22",
                            "NULL",
                            "NULL",
                            "NULL",
                            "NULL",
                            13,
                            "NULL",
                            "NULL",
                            jsonNodes.toString(),
                            "NULL",
                            1
                    );
                    System.out.println(format);
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
        }
	}
}
